/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package cn.edu.dyxy.yinyy.modules.blockly.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import cn.edu.dyxy.yinyy.modules.blockly.entity.BlocklyProject;
import cn.edu.dyxy.yinyy.modules.blockly.service.BlocklyProjectService;

/**
 * Blockly设计项目Controller
 * @author 尹元元
 * @version 2020-03-21
 */
@Controller
@RequestMapping(value = "${adminPath}/blockly/blocklyProject")
public class BlocklyProjectController extends BaseController {

	@Autowired
	private BlocklyProjectService blocklyProjectService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public BlocklyProject get(String id, boolean isNewRecord) {
		return blocklyProjectService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("blockly:blocklyProject:view")
	@RequestMapping(value = {"list", ""})
	public String list(BlocklyProject blocklyProject, Model model) {
		model.addAttribute("blocklyProject", blocklyProject);
		return "modules/blockly/blocklyProjectList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("blockly:blocklyProject:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<BlocklyProject> listData(BlocklyProject blocklyProject, HttpServletRequest request, HttpServletResponse response) {
		blocklyProject.setPage(new Page<>(request, response));
		blocklyProject.setCreateBy(UserUtils.getUser().getUserCode());
		Page<BlocklyProject> page = blocklyProjectService.findPage(blocklyProject);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("blockly:blocklyProject:view")
	@RequestMapping(value = "form")
	public String form(BlocklyProject blocklyProject, Model model) {
		model.addAttribute("blocklyProject", blocklyProject);
		return "modules/blockly/blocklyProjectForm";
	}

	/**
	 * 保存Blockly设计项目
	 */
	@RequiresPermissions("blockly:blocklyProject:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated BlocklyProject blocklyProject) {
		blocklyProjectService.save(blocklyProject);
		return renderResult(Global.TRUE, text("保存Blockly设计项目成功！"));
	}
	
	/**
	 * 删除Blockly设计项目
	 */
	@RequiresPermissions("blockly:blocklyProject:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(BlocklyProject blocklyProject) {
		blocklyProjectService.delete(blocklyProject);
		return renderResult(Global.TRUE, text("删除Blockly设计项目成功！"));
	}

	/**
	 * 启动项目设计
	 */
	@RequiresPermissions("blockly:blocklyProject:edit")
	@RequestMapping(value = "design")
	public String design(BlocklyProject blocklyProject, Model model) {
		blocklyProject = blocklyProjectService.get(blocklyProject);

		//TODO:整理当前项目的参数

		model.addAttribute("setting", blocklyProject);
		model.addAttribute("blocklyProject", blocklyProject);
		model.addAttribute("time", System.currentTimeMillis());

		return "modules/blockly/blocklyProjectDesign";
	}

	/**
	 * 保存Blockly项目的XML代码
	 */
	@RequiresPermissions("blockly:blocklyProject:edit")
	@PostMapping(value = "updateXml")
	@ResponseBody
	public String updateXml(BlocklyProject blocklyProject) {
		BlocklyProject blocklyProject1 = blocklyProjectService.get(blocklyProject.getId());
		blocklyProject1.setCodeXml(blocklyProject.getCodeXml());
		blocklyProjectService.save(blocklyProject1);

		return renderResult(Global.TRUE, text(""));
	}
}